<template>
	<view>
		<view class="s-set-box">
			<s-list-cell padding="0" :lineLeft="false">
				<view class="s-list-cell s-info-box">
					<image :src="user_info.avatar_url" class="s-avatar"></image>
					<view>{{user_info.nick_name}}</view>
				</view>
			</s-list-cell> 
			
			<button hover-class="none" open-type="getPhoneNumber" @getphonenumber="getPhoneNumber" class="special">
				<s-list-cell padding="0" :lineLeft="false" :arrow="true">
					<view class="s-list-cell">
						{{user_info.phone?'手机号：'+user_info.phone:'绑定手机号'}}
					</view>
				</s-list-cell>
			</button>
			
			<s-list-cell padding="0" :lineLeft="false" :arrow="true" @click="address">
				<view class="s-list-cell">
					地址管理
				</view>
			</s-list-cell>
			<view class="s-exit">
				<s-button type="danger" @click="logout" height="88rpx">退出登录</s-button>
			</view>
		</view>	
	</view>
</template>
<script>	
	export default {
		data() {
			return {
				user_info:{}, //我的信息
				codeData:{}
			}
		},
		async onShow() {
			this._onLoad();
		},
		methods: {
			async _onLoad(){
				this.user_info = await this.$s.model.user.set()
				this.codeData = await this.$s.uniApi.wxCode()
			},
			//获取微信手机号
			async getPhoneNumber(e){ 
				const code = this.codeData.code
				const status = await this.$s.uniApi.checkSession()
				if(status.errMsg == "checkSession:ok"){
					var type = 0
					// #ifdef MP-WEIXIN
						type = 1
					// #endif
					// #ifdef MP-TOUTIAO
						type = 2
					// #endif
					if(e.detail.errMsg == 'getPhoneNumber:ok'){
						await this.$s.model.user.sendPhone({
							type:type,
							code:code,
							encryptData:e.detail.encryptedData,
							iv:e.detail.iv
						})
						await this._onLoad()
					}
				}else{
					this.codeData = await this.$s.uniApi.wxCode()
				}
				
			},
			// 地址管理
			async address(){
				return await this.$s.uniApi.navigateTo({url:'../address/address'})
			},
			//退出登录
			async logout(){
				const res = await this.$s.uniApi.showModal({content:'确定要退出吗？'})
				if (res.confirm) {
					await this.$s.model.user.logout()
					await this.$s.uniApi.removeStorage('token')
					await this.$s.uniApi.removeStorage('token_type')
					await this.$s.uniApi.navigateBack({})
				}
			}
		},
		//下拉刷新
		async onPullDownRefresh() {
		    await this._onLoad()
		    uni.stopPullDownRefresh()  
		}
	}
</script>

<style lang="scss" scoped>
@import 'set.scss';
</style>
